Skip to content

Reject Curve25519/Curve448 keys with the wrong length - #825

Open
dlazobnaKocka wants to merge 3 commits into
libtom:developfrom
dlazobnaKocka:fix-ec25519-448-key-length
Open

dlazobnaKocka wants to merge 3 commits into
libtom:developfrom
dlazobnaKocka:fix-ec25519-448-key-length

Conversation

@dlazobnaKocka

@dlazobnaKocka dlazobnaKocka commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

A certificate or SSH2 public key with a short Ed25519, X25519, Ed448 or X448 key reaches the *_import_raw() functions through x509_import_spki() or pem_ssh.c. Those check the length with LTC_ARGCHK. With ARGTYPE 0 that calls abort(), and with ARGTYPE 2 or 3 the memcpy reads past the end of the input buffer. The default Release build (ARGTYPE=4) returns CRYPT_INVALID_ARG.

This SSH2 key with a 1 byte Ed25519 key is enough, passed to pem_decode_openssh() in a build with ARGTYPE 0:

---- BEGIN SSH2 PUBLIC KEY ----
AAAAC3NzaC1lZDI1NTE5AAAAAQE=
---- END SSH2 PUBLIC KEY ----
LTC_ARGCHK 'inlen == 32uL' failure on line 35 of file src/pk/ed25519/ed25519_import_raw.c
Aborted

The PKCS#8 and SubjectPublicKeyInfo importers had a related problem. They only checked the upper bound, so a 1 byte key returned CRYPT_OK and the rest of the key buffer kept whatever was in it before.

The fix returns CRYPT_PK_INVALID_SIZE in all of these cases. Each curve gets a small test with a short PKCS#8, SPKI and raw key. The tests fail on develop and pass with the fix.

Checklist

  • tests are added or updated
  • if this fixes something: added a Fixes: tag to the commit message

The *_import_raw() functions checked the key length with LTC_ARGCHK.
A certificate or SSH public key with a short Ed25519 key could reach
them, so the default build called abort(). With ARGTYPE 2 or 3 the
memcpy read past the end of the input buffer.

The PKCS#8 and SubjectPublicKeyInfo importers only checked the
upper bound. A 1 byte key returned CRYPT_OK and the rest of the key
buffer kept whatever was in it before.

Return CRYPT_PK_INVALID_SIZE in all of these cases.

Fixes: 52a24ca ("add Curve25519 API")
Fixes: 076a110 ("Ed448 + X448")
Fixes: 8d19047 ("Add support for more PEM file types + some fixes")
Fixes: 208ee2b ("Refactor SubjectPublicKeyInfo import")
@karel-m

karel-m commented Nov 17, 2026

Copy link
Copy Markdown
Member

Looks good to me

small correction: the default build via plain make or CMake Release uses ARGTYPE=4 not ARGTYPE=0

@sjaeckel sjaeckel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

Basically good changes, just some suggestions to improve the implementation.

Comment thread src/pk/ec25519/ec25519_import_pkcs8.c Outdated
Comment on lines 39 to 47

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[0]: Sorry, but I don't like that style. Too difficult to understand IMO. Still I very much like the effort to keep the remaining code as is :)

Suggested change
if ((err = der_decode_octet_string(priv_key->data, priv_key->size, key->priv, &key_len)) != CRYPT_OK) {
goto LBL_ERR;
}
if (key_len != sizeof(key->priv)) {
err = CRYPT_PK_INVALID_SIZE;
goto LBL_ERR;
}
fp(key->pub, key->priv);
key->type = PK_PRIVATE;
err = pk_get_pka_id(id, &key->pka);
LBL_ERR:
return err;

Comment thread src/pk/ec448/ec448_import_pkcs8.c Outdated
Comment on lines +41 to +44
if ((err = der_decode_octet_string(priv_key->data, priv_key->size, key->priv, &key_len)) == CRYPT_OK && key_len != priv_len) {
err = CRYPT_PK_INVALID_SIZE;
}
if (err == CRYPT_OK) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as [0]

Comment thread src/pk/ed25519/ed25519_import.c Outdated
Comment on lines +28 to +31
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_ED25519, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK && key_len != sizeof(key->pub)) {
err = CRYPT_PK_INVALID_SIZE;
}
if (err == CRYPT_OK) {

@sjaeckel sjaeckel Nov 18, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[1]: I'd suggest to add a unsigned long public_key_len_should argument to the API of x509_decode_subject_public_key_info() (after public_key_len) and verify inside that function before copying the data.
In case public_key_len_should == 0 don't check.
WDYT?

Comment thread src/pk/ed448/ed448_import.c Outdated
Comment on lines +28 to +31
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_ED448, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK && key_len != sizeof(key->pub)) {
err = CRYPT_PK_INVALID_SIZE;
}
if (err == CRYPT_OK) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as [1]

Comment thread src/pk/x25519/x25519_import.c Outdated
Comment on lines +28 to +31
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_X25519, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK && key_len != sizeof(key->pub)) {
err = CRYPT_PK_INVALID_SIZE;
}
if (err == CRYPT_OK) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as [1]

Comment thread src/pk/x448/x448_import.c Outdated
Comment on lines +28 to +31
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_X448, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK && key_len != 56uL) {
err = CRYPT_PK_INVALID_SIZE;
}
if (err == CRYPT_OK) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as [1]

Comment thread src/pk/ed25519/ed25519_import_raw.c Outdated

if (which == PK_PRIVATE) {
LTC_ARGCHK(inlen == 32uL || inlen == 64uL);
if (inlen != 32uL && inlen != 64uL) return CRYPT_PK_INVALID_SIZE;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC it was decided that those statements stay LTC_ARGCHK'd since release builds don't call abort() and it's helpful when debug builds bail out loud.

This applies to all such changes.

@karel-m please correct me if I'm wrong!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems to me that LTC_ARGCHK here would be a bit more appropriate

@dlazobnaKocka

Copy link
Copy Markdown
Contributor Author

Thanks for the review, and thanks Karel for the ARGTYPE=4 note. You're right, I was looking at the fallback in tomcrypt_cfg.h, but the default Release build returns CRYPT_INVALID_ARG and doesn't abort. I'll fix the description.

I'll switch the PKCS#8 checks to the if/goto form, also in the Curve448 file.

Adding public_key_len_should to x509_decode_subject_public_key_info() sounds good, it's internal so the extra arguement is fine. I'll check the length there before the copy, with 0 meaning no check, and update the Curve25519/448 callers.

About LTC_ARGCHK in *_import_raw: I get the usual rule. I returned an error because x509_import_spki() and the SSH parser pass key bytes from a cert or SSH key into those functions. With ARGTYPE 0 that aborts, and with ARGTYPE 2 or 3 it reads past the end of the buffer. I'd like to keep a real check there, but if you'd rather keep only LTC_ARGCHK, I can move the length check up into the SPKI and SSH callers. What do you prefer?

Add a public_key_len_should argument, 0 means no check. The
Curve25519/Curve448 importers pass their key size, so the check after
the call is not needed anymore.

Also use plain if/goto for the PKCS#8 length checks.

@sjaeckel sjaeckel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update!

[...] with ARGTYPE 2 or 3 [...]

We ignore those two, since they're basically the crazy footgun modes. So there's no need to do any further checks if I'm not missing something and those checks can be reverted to LTC_ARGCHK.

@dlazobnaKocka

Copy link
Copy Markdown
Contributor Author

Thanks, that answers it. I reverted the length checks in the *_import_raw funtions back to LTC_ARGCHK. I also dropped the unit checks that expected those to return an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants